package edu.vt.ece.rt.benchmark.hashmap;

import java.util.ArrayList;
import java.util.Random;

import edu.vt.ece.rt.quorum.ClustersManager;
import edu.vt.ece.rt.quorum.Resource;
import edu.vt.ece.rt.quorum.util.Names;
import edu.vt.ece.rt.util.Logger.Logger;


public class Benchmark extends edu.vt.ece.rt.benchmark.Benchmark {

	private Random random = new Random(hashCode());
	int entryNum = 100;// entries per bucket
	int addition = 0;
	int delete = 0;
	
	@Override
	protected void createLocalObjects() {
		Hashmap.setBuckets(16);
		
		/* local objects count is decided by this num, too rigid!!! */
			for (int bucketNum = 0; bucketNum < 16; bucketNum++) {
					
					/* create bucket and fill entries */
					String bucketID = "bucket-"+bucketNum;
					Hashmap h = new Hashmap(bucketID);
					addEntries(h, bucketNum, localObjectsCount);
					Logger.debug("Created bucket "+bucketID);
		}
			
		Hashmap h = new Hashmap("global");		
	}

	
	@Override
	protected Object randomId() {
		return null;
	}
	
	
	public void addEntries(Hashmap h, int bucketNum, int localObjects) {
		
		for (int i = 0; i< localObjects;i++) {
			if (Hashmap.hash(i) == bucketNum){
				h.getList().add(new Integer(i));
			}
		}
	}
	@Override
	protected int getOperandsCount() {
		return 0;
	}

	@Override
	protected void readOperation(Object... ids) {
		
		int val = random.nextInt(localObjectsCount);
//		System.out.println("Finding "+val);
		Hashmap.find(val, null);
		
	}

	@Override
	protected void writeOperation(Object... ids) {
		
		Hashmap.closedNestedHashmap(localObjectsCount, nesting);
//		int val = random.nextInt(localObjectsCount);
//		Boolean delOp = false;
//		
//		if (random.nextInt(100) > 50) {
//			delOp = true;
//		}
//		
//		if (delOp) {
//			Logger.debug("delete op started on "+ClustersManager.baseClusterId);
//			boolean del = Hashmap.delete(val, null);
//			if (del) {
////				System.out.println("Deleting "+val);
//				delete= delete - val;
////				System.out.println("Deleted "+val);
//			}	
//		} else {
//			boolean add = Hashmap.add(val, null);
//			if (add) {
////				System.out.println("Adding "+val);
//				addition = addition + val;
////				System.out.println("Added "+val);
//			}
//		}
	}

	@Override
	protected void checkSanity() {
		Hashmap.checkBuckets(null);
	}


	@Override
	protected String getLabel() {
		return "hashmap-qr";
	}
}
